package com.priusoft.api.service;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

//@Service
public class RabbitMQTestService {

    @RabbitListener(queues = "${mq.queue}")
    public void receive(String payload, Channel channel,
                        @Header(AmqpHeaders.DELIVERY_TAG) long tag) {
        System.out.println("消费内容为：" + payload);
        try {
            // 确认消息
            channel.basicAck(tag, false);

            // 拒绝消息，消息会被丢弃，不会重回队列
            // channel.basicReject(tag,false);

            // 否认消息，消息重新入队列，然后重新消费
            // channel.basicNack(tag,false,true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
